package vn.bang.xiangqi.components;

import java.util.Collection;

import vn.bang.xiangqi.infrastructure.Position;
import vn.bang.xiangqi.infrastructure.Vector;
import vn.bang.xiangqi.utility.BoardUtil;

public class Pawn extends AbstractBaseNode {
	public Pawn() {
		super();
	}

	public Pawn(Position originalPosition) {
		super(originalPosition);
	}

	private static Vector[] PAWN_PASSED_VECTORS = { new Vector(-1, 0),
			new Vector(+1, 0), new Vector(0, +1), };

	@Override
	protected boolean notPrevented(Vector vector, Collection<INode> ours,
			Collection<INode> enemies) {
		Position destinationPosition = new Position();
		destinationPosition.setColumnIndex(this.GetCurrentPosition()
				.getColumnIndex() + vector.getColumn());
		destinationPosition.setRowIndex(this.GetCurrentPosition().getRowIndex()
				+ vector.getRow());

		INode nodeResult = null;
		for (INode node : ours) {
			if (node.PositionAt(destinationPosition)) {
				nodeResult = node;
				break;
			}
		}

		return true;
	}

	@Override
	protected Vector retrieveVectorMovingTo(Position destinationPosition) {
		if (null == destinationPosition
				|| !BoardUtil.IsInBoard(destinationPosition)) {
			return null;
		}

		Vector vector = null;

		if (position.getRowIndex() > 5) {
			vector = retrieveVectorValid(destinationPosition,
					PAWN_PASSED_VECTORS);
		} else {
			int vectorColumn = 0;
			int vectorRow = +1;

			if (position.getColumnIndex() + vectorColumn == destinationPosition
					.getColumnIndex()
					&& position.getRowIndex() + vectorRow == destinationPosition
							.getRowIndex()) {
				vector = new Vector(vectorColumn, vectorRow);
			}
		}

		return vector;
	}

}